<template>
  <a-modal :title="title" :after-close="reset" @ok="submit">
    <a-form
      ref="formRef"
      :model="form"
      :rules="rules"
      :label-col="{ style: { width: '80px' } }"
    >
      <a-form-item label="分类名称" name="name">
        <a-input v-model:value="form.name" placeholder="分类名称" />
      </a-form-item>
      <a-form-item label="URL" name="url">
        <a-input v-model:value="form.url" placeholder="url" />
      </a-form-item>
      <a-form-item label="图标" name="extra">
        <upload-img v-model="form.extra" placeholder="门店logo" />
      </a-form-item>
      <a-form-item label="排序" name="sort">
        <a-input v-model:value="form.sort" placeholder="排序" />
      </a-form-item>
      <a-form-item label="状态" name="status">
        <a-radio-group v-model:value="form.status">
          <a-radio :value="true"> 显示 </a-radio>
          <a-radio :value="false"> 隐藏 </a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="扩展字段" name="extra">
        <a-input v-model:value="form.extra" placeholder="扩展字段" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script setup lang="ts">
import { categoryAddup, categoryinfo } from './api';
import { category, type Icategory } from './entity';
import { validReg } from '@/utils/validate';

const props = withDefaults(defineProps<{
  data: Partial<Icategory>
    type:number
}>(),{
})
const title = computed(() => props.data.id ? '编辑' : '新增')
const { formRef, submit, close } = useDialog({ validCallback: save })

let form = $ref(new category())
watchEffect(() => {
  if (props.data.id) {
    categoryinfo(props.data.id).then(res => {
      form.setProps(res)
    })
  }
})

const rules = {
        categoryName: [validReg('required', '请输入用户名')]
}

onMounted(() => {
})
const emit = defineEmits(['change'])
function save() {
    categoryAddup({...form,type:props.type}).then(() => {
    AntMessage.success('保存成功')
    close()
    emit('change')
  })
}

function reset() {
  form = new category()
}
</script>

<style lang="less"></style>
